/*
 * @(#)PanelQueryResults.java Jan o1, 2012 - 7:15:22 PM
 * 
 * Copyright 2011 Cin/UFPE/VSPEED
 */
package br.ufpe.cin.vspeed.gui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.layout.GroupLayout;

import speed2010.reformulacao.query.InstanceResult;
import br.ufpe.cin.vspeed.gui.render.ButtonEditor;
import br.ufpe.cin.vspeed.gui.render.CellRenderInstanceInformation;

/**
 * @author <a href="mailto:andreza.lba@gmail.com">Andreza Leite de Alencar</a>
 */
public class PanelQueryResults extends javax.swing.JPanel {
	private static final long serialVersionUID = 5406046836680282001L;

	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private DefaultTableModel tableModel;

	private Map<Integer, InstanceResult> resultsMap;

	/**
	 * Creates new form JPanelQueryResults2
	 */
	public PanelQueryResults() {
		initGUI();
		updateRows(new ArrayList<InstanceResult>());
	}

	/**
	 * Changes the value of resultsMap as the parameter.
	 * 
	 * @param resultsMap
	 */
	public void setResultsMap(final Map<Integer, InstanceResult> resultsMap) {
		this.resultsMap = resultsMap;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 */
	private void initGUI() {
		this.jScrollPane1 = new javax.swing.JScrollPane();
		this.jTable1 = new javax.swing.JTable();

		setBackground(new java.awt.Color(255, 255, 255));

		this.tableModel = updateRows(new ArrayList<InstanceResult>());
		this.jTable1.setModel(this.tableModel);

		this.jTable1.setColumnSelectionAllowed(true);
		this.jTable1.setSelectionBackground(new java.awt.Color(153, 204, 255));
		this.jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
		this.jTable1.getTableHeader().setReorderingAllowed(false);
		this.jScrollPane1.setViewportView(this.jTable1);

		final GroupLayout layout = new GroupLayout(this);
		setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.LEADING).add(
				layout.createSequentialGroup().addContainerGap()
						.add(this.jScrollPane1, GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.LEADING).add(
				layout.createSequentialGroup().addContainerGap()
						.add(this.jScrollPane1, GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE).addContainerGap()));
	}

	public DefaultTableModel updateRows(final List<InstanceResult> instances) {
		final String[][] lines = buildConceptsArray(instances);

		final String[] columns = new String[]{"Result", "Source", ""};
		final DefaultTableModel paramTableModel = new DefaultTableModel(lines, columns) {
			private static final long serialVersionUID = -1507078005332093940L;

			Class<?>[] types = new Class[]{String.class, String.class, String.class};
			boolean[] canEdit = new boolean[]{false, false, true};

			@Override
			public Class<?> getColumnClass(final int columnIndex) {
				return this.types[columnIndex];
			}

			@Override
			public boolean isCellEditable(final int rowIndex, final int columnIndex) {
				return this.canEdit[columnIndex];
			}
		};
		this.jTable1.setModel(paramTableModel);

		this.jTable1.getColumnModel().getSelectionModel()
				.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
		this.jTable1.getColumnModel().getColumn(0).setResizable(false);
		this.jTable1.getColumnModel().getColumn(0).setHeaderValue("Result");
		this.jTable1.getColumnModel().getColumn(1).setResizable(false);
		this.jTable1.getColumnModel().getColumn(1).setHeaderValue("Source");
		this.jTable1.getColumnModel().getColumn(2).setMinWidth(30);
		this.jTable1.getColumnModel().getColumn(2).setPreferredWidth(30);
		this.jTable1.getColumnModel().getColumn(2).setMaxWidth(30);
		this.jTable1.getColumnModel().getColumn(2).setHeaderValue("");
		this.jTable1.getColumnModel().getColumn(2).setCellRenderer(new CellRenderInstanceInformation());
		this.jTable1.getColumnModel().getColumn(2).setCellEditor(new ButtonEditor(new JCheckBox(), this.resultsMap));
		this.jTable1.setColumnSelectionAllowed(false);
		this.jTable1.setRowSelectionAllowed(false);
		this.jTable1.setColumnSelectionAllowed(false);
		this.jTable1.setRowSelectionAllowed(false);
		this.jTable1.setColumnSelectionAllowed(false);
		this.jTable1.setRowSelectionAllowed(false);

		return paramTableModel;
	}

	private String[][] buildConceptsArray(final List<InstanceResult> instances) {
		String data[][] = new String[][]{};
		if (!instances.isEmpty()) {
			final int linesNumber = instances.size();
			data = new String[linesNumber][3];

			for (int i = 0; i < instances.size(); i++) {
				data[i][0] = instances.get(i).getInstanceValue();
				data[i][1] = instances.get(i).getSourceType() + " " + instances.get(i).getSourceAddress();
				data[i][2] = "";
			}
		}

		return data;
	}

	public static void main(final String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				final JFrame frame = new JFrame();
				final PanelQueryResults paramComponent = new PanelQueryResults();
				frame.getContentPane().add(paramComponent);
				frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
				frame.pack();
				frame.setVisible(true);
			}
		});
	}
}